$('.select2').select2();
// $.get("header_menu.html",function(data){ $("#header").html(data); });
$.get("footer.html",function(data){ $("#footer").html(data); });

$(document).ready(function(){
    // $("#product").addClass("active");
    // $("#product2").addClass("active");

    $('.check1').click(function(){
        getData(1);
    });
    function getData(curr){
        $.ajax({
            type:"get",
            url:"http://114.116.72.220:9999/AllProducts/",
            async:true,
            data:{
                page:curr,
                row: 10,
                keyword:$('#input1').val(),
                status: 2
            },
            success:function(data){
                console.log(data);
                if(data[0].length==0){
                    layer.msg('暂无数据');
                }
                TableView(data[0]);
                pageDivd(curr,data[1]);

            }
        });
    }
    getData(1);
    function pageDivd(curr,page_num){
        layui.use(['laypage', 'layer'], function(){
            var laypage = layui.laypage
                ,layer = layui.layer;
            laypage.render({
                elem: 'demo7',
                count: page_num,
                limit: 10,
                curr:curr,
                jump: function(obj,first){
                    if(!first){
                        getData(obj.curr);
                    }
                }
            });
        })
    }
    pageDivd(1,9);
    function TableView(data){
        $('.table_data').html('');
        for (var i=0;i<data.length;i++) {

            var trText = `<tr class="${i%2 ? '' : 'tr_back'}">
		      		    		<td>${i+1}</td>
		      		    		<td>
		      		    			<img src="${data[i].product_thumbnail}" height="50"/>
		      		    		</td>
		      		    		<td>${data[i].product_no}</td>
		      		    		<td>${data[i].product_name}</td>
		      		    		<!--<td>${data[i].product_stock}</td>-->
		      		    		<td>
		      		    			￥${data[i].product_price}
		      		    		</td>
		      		 
		      		    		<td index="${data[i].product_id}" class="table_btn_box">
								  <button class="table_btn detail">查看</button>
								  <!--<button class="table_btn add_btn">新增</button>-->
								  <button class="table_btn edit_btn">修改</button>
								  <button class="table_btn go_down">删除</button>
		      		    		</td>
		      		    	</tr>`;

            $('.table_data').append(trText) ;

        }

        lookDetail();
    }

    function lookDetail(){
        $('.detail').click(function(){
            var Index = $(this).parent().attr('index');
            console.log(Index);
            window.location.href="http://114.116.72.220:9010/src/product_details.html?productId="+Index;
        });
        // $('.add_btn').click(function(){
        // 	var Index = $(this).parent().attr('index');
        // 	window.location.href="http://114.116.72.220:9999/AddProductNumber/?productId="+Index;
        // });
        $('.edit_btn').click(function(){
            var Index = $(this).parent().attr('index');
            window.location.href="http://114.116.72.220:9010/src/new_product2.html?productId="+Index;
        });
        $('.go_down').click(function(){
            var Index = $(this).parent().attr('index');
            layer.confirm('确定要删除此商品吗？', {
                title: ['提示'],
                btn: ['确定','取消'] //按钮
            }, function(){
                $.ajax({
                    type:"get",
                    url:"http://114.116.72.220:9999/DeleteProduct/",
                    async:true,
                    data:{
                        productId:Index
                    },
                    success:function(data){
                        if(data=='success'){
                            layer.msg('成功删除',{time:500},function(){
                                location.reload();
                            })
                        }else{
                            layer.msg('操作失败');
                        }

                    }

                })
            }, function(){});
        });

    }

    // $('#input1').blur(function () {
    //   if($('#input1').val() == ''){
    //   	getData(1)
    //   }
    // })
});
